package com.ruoyi.web.controller.applet.controller;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.domain.SysAppBookchapters;
import com.ruoyi.system.domain.SysAppMedicine;
import com.ruoyi.system.domain.SysAppPrescriptionSort;
import com.ruoyi.system.domain.SysDictData;
import com.ruoyi.system.dto.MedicineDicDataDTO;
import com.ruoyi.system.dto.PrescriptionDicDataDTO;
import com.ruoyi.system.service.ISysAppMedicineService;
import com.ruoyi.system.service.ISysDictTypeService;

/**
 * 药物管理相关模块
 * @author gyp
 *
 */
@Controller
@RequestMapping("/applet/medicine")
public class MedicineController {
	
	@Autowired
    private ISysAppMedicineService sysAppMedicineService;
	
	@Autowired
	private ISysDictTypeService dictTypeService;
	
	/**
	 * 获取中药列表信息
	 * @return
	 */
	@PostMapping("/selectMedicines")
	@ResponseBody
	public AjaxResult selectMedicines(String keyWord) {
		Map<String, Object> map = Maps.newConcurrentMap();
		List<SysDictData> dictDatas = dictTypeService.selectDictDataByType("sys_medicine_class");
		SysAppMedicine sysAppMedicine = new SysAppMedicine();
		sysAppMedicine.setState(1);
		sysAppMedicine.setMedicineName(keyWord);
		List<SysAppMedicine> medicines = sysAppMedicineService.selectSysAppMedicineList(sysAppMedicine);
		MedicineDicDataDTO medicineDicDataDTO = null;
		List<MedicineDicDataDTO> params = Lists.newArrayList();
		for (SysDictData sysDictData : dictDatas) {
			medicineDicDataDTO = new MedicineDicDataDTO();
			BeanUtils.copyBeanProp(medicineDicDataDTO, sysDictData);
			List<SysAppMedicine> medicineList = medicines.stream().filter(s -> s.getMedicineClass().equals(sysDictData.getDictLabel()))
					.collect(Collectors.toList()); 
			medicineDicDataDTO.setMedicineList(medicineList);
			params.add(medicineDicDataDTO);
		}
		map.put("medicines", params);
		return AjaxResult.success("操作成功", map);
	} 
	
	/**
	 * 根据id获取详情信息
	 * @param id
	 * @return
	 */
	@PostMapping("/selectMedicineById")
	@ResponseBody
	public AjaxResult selectMedicineById(Long id) {
		Map<String, Object> map = Maps.newConcurrentMap();
		SysAppMedicine medicine = sysAppMedicineService.selectSysAppMedicineById(id);
		map.put("medicineInfo", medicine);
		return AjaxResult.success("操作成功", map);
	} 
	

}
